package com.wobangkj.common.api.crud;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wobangkj.common.model.crud.User;
import com.wobangkj.common.service.*;
import com.wobangkj.common.service.UserConstantInterface;
import com.wobangkj.common.util.wx.JsonUtils;
import com.wobangkj.common.model.crud.UserDe;
import com.wobangkj.common.util.wx.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/user")

public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping(value = "/create")
    public Object create(@RequestBody User data) {
        // 创建时间
        data.setCreatetime(Timestamp.valueOf(LocalDateTime.now()));
        return userService.create(data);
    }

    @PostMapping(value = "/login")
    public Object login(User data,String code) {

        Map<String,String> param=new HashMap<>();
        param.put("appid", UserConstantInterface.WX_LOGIN_APPID);
        param.put("secret",UserConstantInterface.WX_LOGIN_SECRET);
        param.put("js_code",code);
        param.put("grant_type",UserConstantInterface.WX_LOGIN_GRANT_TYPE);
        // 发送请求
        String wxResult = HttpClientUtil.doGet(UserConstantInterface.WX_LOGIN_URL, param);
        JSONObject jsonObject = JSONObject.parseObject(wxResult);
        // 获取参数返回的
        String open_id = jsonObject.get("openid").toString();
        User user =  userService.getByOpenid(open_id);
        String skey = UUID.randomUUID().toString();
        if(user!= null){
            user.setSkey(skey);
            userService.update(user);
            Map<String,Object> model=new HashMap<>();
            model.put("id",user.getId().toString());
            model.put("isMaster",user.getIsMaster());
            return model;
        }else{
            data.setOpenid(open_id);
            data.setSkey(skey);
            data.setCreatetime(Timestamp.valueOf(LocalDateTime.now()));
            userService.create(data);
            Map<String,Object> model=new HashMap<>();
            model.put("id",data.getId().toString());
            model.put("isMaster",data.getIsMaster());
            return model;
        }
    }


    @PutMapping(value = "/update")
    public Object update(@RequestBody User user) {

        return userService.update(user);
    }

    @DeleteMapping(value = "/delete/{id}")
    public Object delete(@PathVariable(value = "id") Long id) {
        return userService.delete(id);
    }

    @GetMapping("/id")
    public Object id(Long id) {

        return userService.id(id);
    }


    @GetMapping("/search")
    public Object search(@RequestParam(required = false) Map<String, Object> params, User user) {
        return userService.search(params, user);
    }

    @GetMapping("/idDe")
    public Object idDe(Long id) {

        return userService.idDe(id);
    }

    @GetMapping("/searchDe")
    public Object searchDe(@RequestParam(required = false) Map<String, Object> params, UserDe user) {
        return userService.searchDe(params, user);
    }


//模糊查询

    @GetMapping("/findByNameLike/{name}")
    public List<User> findByNameLike(@PathVariable(value = "name") String name){
        // 一定要加 "%"+参数名+"%"
        return userService.findByNameLike(name);
    }

    @PostMapping(value = "/findMyCollect")
    public Object findMy(String id, Long collect_class){
        if(collect_class==0){
            Long[] jobId= userService.findMyJob(id);
            Map<String, Object> modelMap = new HashMap();
            for(int i=0;i<jobId.length;i++)
            {
                modelMap.put(""+i+"",userService.jobIdList(jobId[i]));
            }
            return  modelMap;

        }
        else {
            Long[] shareId= userService.findMyShare(id);
            Map<String, Object> modelMap = new HashMap();
            for(int i=0;i<shareId.length;i++)
            {
                modelMap.put(""+i+"",userService.shareIdList(shareId[i]));
            }
            return modelMap;
        }
    }
}
